package com.scedu.controller;

import com.scedu.domain.ProductCategory;
import com.scedu.domain.ProductInfo;
import com.scedu.dto.OrderDTO;
import com.scedu.enums.ResultEnums;
import com.scedu.exception.SellException;
import com.scedu.form.ProductForm;
import com.scedu.service.ProductCategoryService;
import com.scedu.service.ProductInfoService;
import com.scedu.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/seller/product")
public class SellerProductController {
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    ProductCategoryService productCategoryService;

    /*
   卖家端商品列表
    */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map){
        PageRequest pageRequest= PageRequest.of(page-1,size);
        Page<ProductInfo> productInfoPage= productInfoService.findAll(pageRequest);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("productInfoPage",productInfoPage);
        return  new ModelAndView("product/list",map);
    }
    /*
    下架
     */
    @GetMapping("/off_sale")
    public ModelAndView off_sale (@RequestParam("productId") String productId,
                                Map<String,Object> map){
        try{

            productInfoService.downsale(productId);
        }
        catch ( SellException e){
            log.error("商品不存在,productId={}",productId);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);
    }
    /*
    上架
     */
    @GetMapping("/on_sale")
    public ModelAndView on_sale (@RequestParam("productId") String productId,
                                  Map<String,Object> map){
        try{

            productInfoService.onsale(productId);
        }
        catch ( SellException e){
            log.error("商品不存在,productId={}",productId);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);
    }
    /*
   详情
    */
    @GetMapping("/index")
    public ModelAndView index (@RequestParam(value = "productId",required = false) String productId,
                                 Map<String,Object> map){
        List<ProductCategory> productCategoryList=productCategoryService.findAll();
        map.put("categoryList",productCategoryList);

        if (!StringUtils.isEmpty(productId)) {
            try {

                ProductInfo productInfo = productInfoService.findone(productId);
                map.put("productInfo", productInfo);

            } catch (SellException e) {
                log.error("商品不存在,productId={}", productId);
                map.put("msg", e.getMessage());
                map.put("url", "/sell/seller/product/list");
                return new ModelAndView("common/error", map);
            }
        }else{
            ProductInfo productInfo =new ProductInfo();
            map.put("productInfo", productInfo);
        }
        return new ModelAndView("/product/index",map);
    }
    /*
  修改
   */
    @PostMapping("/save")
    public ModelAndView save (@Valid ProductForm productForm, BindingResult bindingresult,
                              Map<String,Object> map){
        if (bindingresult.hasErrors()){
            //有验证错误
            log.error("表单创建错误 ，参数错误 productInfo={}" ,productForm);
            //throw new SellException(ResultEnums.ORDERS_FORM_ERROR);
            map.put("msg",  bindingresult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/product/index");
            return new ModelAndView("common/error", map);

            //return ResultUtil.fail(100,bindingresult.getFieldError().getDefaultMessage());
            //return  null;
        }
        ProductInfo productInfo=new ProductInfo();
        try {
            //根据productId判断是否是新增
            if (StringUtils.isEmpty(productForm.getProductId())){
                //新商品，生成ID
                productForm.setProductId(KeyUtil.genUniqueKey());
            }else{
                productInfo=productInfoService.findone(productForm.getProductId());
            }

            BeanUtils.copyProperties(productForm,productInfo);
            productInfoService.save(productInfo);
        }catch (SellException e){
            log.error("商品保存错误,productForm={}", productForm);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/product/index");
            return new ModelAndView("common/error", map);
        }

        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);

    }
}
